package com.gongpingjialoan.adapter.car;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.bean.AssessHistory;
import com.gongpingjialoan.imageload.ImageLoad;

import java.util.List;

public class FreeSellAdapter extends BaseAdapter {

    private List<AssessHistory> mAssessHistories;

    private Context context;

    private LayoutInflater layoutInflater;

    public FreeSellAdapter(Context context , List<AssessHistory> mAssessHistories) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
        this.mAssessHistories = mAssessHistories;
    }


    public void setData(List<AssessHistory> mAssessHistories) {
        this.mAssessHistories = mAssessHistories;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mAssessHistories.size();
    }

    @Override
    public AssessHistory getItem(int position) {
        return mAssessHistories.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.free_slee_item, null);
            ViewHolder viewHolder = new ViewHolder();
            viewHolder.pic = (ImageView) convertView.findViewById(R.id.pic);
            viewHolder.carName = (TextView) convertView.findViewById(R.id.carName);
            viewHolder.des = (TextView) convertView.findViewById(R.id.des);
            convertView.setTag(R.id.tag_first , viewHolder);
        }
        initializeViews(mAssessHistories.get(position), (ViewHolder) convertView.getTag(R.id.tag_first));
        convertView.setTag(R.id.tag_second, mAssessHistories.get(position));
        return convertView;
    }

    private void initializeViews(AssessHistory assessHistory, ViewHolder holder) {
        ImageLoad.LoadImage(holder.pic, assessHistory.url, R.drawable.car_loading, R.drawable.car_no,context);
        holder.carName.setText(assessHistory.model + assessHistory.model_detail);
        holder.des.setText( assessHistory.mile + " 万公里/" + assessHistory.year + "年上牌");

    }

    protected class ViewHolder {
        private ImageView pic;
        private TextView carName;
        private TextView des;
    }
}
